Fix PictureBox frame callback handle race in OnFrameChanged - #14826
Fix PictureBox frame callback handle race in OnFrameChanged#14826LeafShi1 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses an intermittent Win32Exception: Error creating window handle seen during PictureBox animated image frame callbacks by reducing handle-state race exposure in OnFrameChanged.
Changes:
- Reordered the conditional in
PictureBox.OnFrameChangedto checkIsHandleCreatedbefore evaluatingInvokeRequired, avoiding the more complexInvokeRequiredpath when there is no handle.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Looks good to me! |
|
There is one request in general I have: Let's please create a document with breaking changes. And I mention this in this context, since we need to make sure, that we pursue different approaches to transfer knowledge to Copilot Agents in the right moment and context, so - for example - if they are tasks with a migration from NetFx to a .NET based TFM, they also take new options and most of all changed behaviors into account. What has this to do with this PR? Well, this is a bug fix, but one which is also present in NetFx. So even if it is niche, it's automatically a potential breaking change, and that should be enough to include it in the list. If it then really has the potential to affect what a Copilot-driven migration/modernization would yield is bottom line for the Agent in charge to decide. But it needs to knowledge to base its decision on. And that's why we need that centralized information, which should become part of the WinForms repo. This is not a call to action for now to anybody. @Shyam-Gupta, @Olina-Zhang, @LeafShi1, @SimonZhao888, @ricardobossan. |
That makes sense. I agree that even long-standing .NET Framework behaviors that appear to be edge cases should be tracked when fixed, as they may become migration-relevant breaking changes. Centralizing this information would also allow future modernization tooling and Copilot-driven migration workflows to take such behavioral differences into account when assessing or upgrading applications. I've added the |
Fixes #14823
Root Cause
During animated image updates,
OnFrameChangedcould evaluateInvokeRequiredbefore checkingIsHandleCreated.In rare timing windows (handle create/destroy while animation callback is running), this can lead to unstable handle state and an occasional "Error creating window handle".
Proposed changes
PictureBox.OnFrameChanged, checkIsHandleCreatedfirst, thenInvokeRequired.This allows for an immediate short-circuit when the handle does not exist, avoiding the more complex
InvokeRequiredpath (which involves parent chain traversal, thread checks, and race conditions regarding handle state).It also ensures consistency with similar controls, such as
LabelandButtonBase, which likewise checkIsHandleCreatedfirst.Customer Impact
Regression?
Risk
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow